package cn.xing.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;

import cn.xing.model.Data;
import cn.xing.utils.JedisAdapter;

@Controller
public class IndexController {

	@Autowired
	private JedisAdapter jedis;

	private List<Data> toResult(List<String> list) {
		List<Data> result = new ArrayList<Data>();
		for (String json : list) {
			Data data = JSON.parseObject(json, Data.class);
			result.add(data);
		}
		return result;
	}

	@RequestMapping("/")
	public String index(Model model) {
		return "redirect:/index/weibo";
	}

	@RequestMapping("/index/{path}")
	public String index(Model model, @PathVariable String path) {
		if ("weibo".equals(path)) {
			List<String> weibo = jedis.lrange("weibo_hot", 0, -1);
			List<Data> result = toResult(weibo);
			model.addAttribute("hot", result);
			model.addAttribute("flag", "weibo");
		} else if ("bilibili".equals(path)) {
			List<String> bilibili = jedis.lrange("bili_hot", 0, -1);
			List<Data> result = toResult(bilibili);
			model.addAttribute("hot", result);
			model.addAttribute("flag", "bilibili");
		} else if ("about".equals(path)) {
			String s = "一个无聊的灵魂<br>人生一世，过完就好。";
			model.addAttribute("about", s);
			model.addAttribute("flag", "about");
		}
		return "index";
	}

}
